/*------------------------------------------------------------------------------
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
--------------------------------------------------------------------------------
This program was written for Afflatus Techtronics - Resource section.

Project 			: USART communication on PIC18Fxxx
File                : usart.c
Code type	        : Code Module

Controller	        : PIC18Fxxx
XTAL frequency     	: 8MHz [Internal]
Compiler and IDE	: MCC18 with MPLab IDE

Author  			: Ashwin Vijayakumar
Current Occupation  : Computer Engineering Graduate student @ WMU, Kalamazoo
                          , Michigan

Start Date			: Jan-15-2010
Validation Date    	: Jan-15-2010

--------------------------------------------------------------------------------
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
------------------------------------------------------------------------------*/

#include <p18cxxx.h>
#include <usart.h>

// -----------------------------------------------------------------------------
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// USART Initialization ~~~~~~~~~~~~~~~~~
// -----------------------------------------------------------------------------

void Usart_Initialize(void)
{
  	TXSTAbits.TXEN = 1;					// Transmit enable
	RCSTAbits.CREN = 1;					// Continuous receive enable
	TRISCbits.TRISC7 = 1;				// Set RX pin as input
	TRISCbits.TRISC6 = 0;				// Set TX pin as output
	//TXSTAbits.BRGH = 1;
	SPBRG = 12;							// Baud rate [9600 @ 8MHz]
	RCSTAbits.SPEN = 1;					// Serial port enable                      
}

// -----------------------------------------------------------------------------
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// USART Initialization ~~~~~~~~~~~~~~~~~
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// USART Transmit Routine ~~~~~~~~~~~~~~~
// -----------------------------------------------------------------------------

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Send a character ~~~~~~~~~~~~~~~~~~~~

void Usart_putc(unsigned char character)
{
  	while (PIR1bits.TXIF == 0);	    	// Wait till Tx buffer is empty
	TXREG = character;					// Load TX buffer with the character
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Send a string of characters ~~~~~~~~~

void Usart_puts(unsigned char *string)  // Send a null terminated string of 
                                          // charaters via the serial port 
                                          // "Usart_puts('null terminated 
                                          // source string')"
{								   
   while(*string!='\0')                	// Keep sending until 'NULL' is 
                                          // encountered.
   {
    	Usart_putc(*string);	        // Send character by character
    	string++;			  			// Increment the pointer to string
   }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Send a string of characters ~~~~~~~~~

void Usart_putrs(const rom char *string)  // Send a null terminated string of 
                                          // charaters via the serial port 
                                          // "Usart_puts('null terminated 
                                          // source string')"
{								   
   while(*string!='\0')                	// Keep sending until 'NULL' is 
                                          // encountered.
   {
    	Usart_putc(*string);	        // Send character by character
    	string++;			  			// Increment the pointer to string
   }
}

// -----------------------------------------------------------------------------
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// USART Transmit Routine ~~~~~~~~~~~~~~~
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// USART Receive Routine ~~~~~~~~~~~~~~~~
// -----------------------------------------------------------------------------

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Receive a character ~~~~~~~~~~~~~~~~~
      
unsigned char Usart_getc(void)
{
  while (PIR1bits.RCIF == 0);	    	// Wait till Tx buffer is empty
  return RCREG;                     	// Load the character with RX buffer
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Receive a string of characters ~~~~~~

void Usart_gets(unsigned char *str, unsigned int len)
{  
   while(len-- != 0)
   {
      *str=Usart_getc();
	  
	  if (*str=='\0')					// Stop accepting data if NULL character 
                                          // is received		
	    break;

	  else if(*str==0x1b)				// Stop accepting data if 'Esc' is pressed
	    break;

      str++;							// Point to the next char space in the 
                                          // string
   }
   
   *str='\0';			        		// Terminate the received string with a 
                                          // NULL caharter
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Receive interrupt routine ~~~~~~~~~~~


// -----------------------------------------------------------------------------
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// USART Receive Routine ~~~~~~~~~~~~~~~~
// -----------------------------------------------------------------------------
